<?php
namespace Personnelsystem\Controller;

use Think\Controller;

require('lib/PHPExcel.php');
require('lib/PHPExcel/IOFactory.php');

header("content-type:text/html;charset=utf-8");

class ManageController extends LoginTrueController
{
    public function __construct()
    {
        parent::__construct();
        $this->LoginIsSystem();
    }
    public function ManageLists()
    {

        $this->LoginTrue();
        $admin = M("admin");
        $rs_admin = $admin->select();
        $this->assign("rs_admin", $rs_admin);
        $sessionPowers = session("aPowers");
        $this->assign("sessionPowers", $sessionPowers);
        $sessionaId = session("aId");

        $this->assign("sessionaId", $sessionaId);
        $system = M("system");
        $rs_system = $system->field("sErrorPwdLockNum")
            ->where("sId=1")
            ->find();
        $sEPLN = $rs_system["sErrorPwdLockNum"];
        $this->assign("systemEPLN", $sEPLN);
        $this->display();
    }

    public function ManageAdd()
    {
        $this->LoginTrue();
        $regione=M("regione");
        $regiones=$regione->where("pId='650000000000'")->select();//市级
        $this->assign("regiones", $regiones);
        $this->display();
    }

    public function ManageAddAction()
    {
        $this->LoginTrue();
        $admin = M("admin");
        $rs_admin = $admin->select();
        $data["aUser"] = $_POST["aUser"];//登录昵称
        
        $data["aName"] = $_POST["aName"];
        $data["aPwd"] = md5($_POST["aPwd"]);
        $data["aSex"] = $_POST["aSex"];
        $data["aPowers"] =$_POST["aPowers"];
        $data["aTel"] = $_POST["aTel"];
        $data["aEmail"] = $_POST["aEmail"];
        if ($data["aEmail"] == "") {
            $data["aEmail"] == null;
        }
        foreach ($rs_admin as $val_admin) {
            if ($data["aUser"] == $val_admin["aUser"]) {
                $this->error("登陆账号已存在，不得重复");
            } elseif ($data["aTel"] == $val_admin["aTel"]) {
                $this->error("手机号码已经存在，不得重复");
            } elseif ($data["aEmail"] != "") {
                if ($data["aEmail"] == $val_admin["aEmail"]) {
                    $this->error("邮箱已存在，不得重复");
                }
            }
        }

        $regioneid=$_POST["regione1"];  //地区选择
        if(!empty($_POST["regione2"])){
            $regioneid=$_POST["regione2"];
        }
        if(!empty($_POST["regione3"])){
            $regioneid=$_POST["regione3"];
        }
        if(!empty($_POST["regione4"])){
            $regioneid=$_POST["regione4"];
        }
        $data['manageRegione']=$regioneid;
        $result = $admin->add($data);
        if ($result) {
            $this->success("添加管理员成功",U("managelists"));
        } else {
            $this->error("添加管理员失败");
        }
    }

    public function ManageUpdate()
    {
        $this->LoginTrue();
        $aId = $_GET["aId"];
        $admin = M("admin");
        $rs_admin = $admin->where("aId={$aId}")->find();
        $this->assign("rs_admin", $rs_admin);

        $regione=M("regione");
        $regiones=$regione->where("pId='650000000000'")->select();//市级
        $this->assign("regiones", $regiones);

        $this->display();
    }

    public function ManageUpdateAction()
    {
        $this->LoginTrue();
        $aId = $_GET["aId"];

        $admin = M("admin");
        $data["aName"] = $_POST["aName"];
        $data["aSex"] = $_POST["aSex"];
        $data["aTel"] = $_POST["aTel"];
        $data["aEmail"] = $_POST["aEmail"];
        if ($data["aEmail"] == "") {
            $data["aEmail"] = null;
        }
        $rs_admin = $admin->where("aId!={$aId}")->select();
        foreach ($rs_admin as $val_admin) {
            if ($data["aTel"] == $val_admin["aTel"]) {
                $this->error("手机号码已经存在，不得重复");
            } elseif ($data["aEmail"] != "") {
                if ($data["aEmail"] == $val_admin["aEmail"]) {
                    $this->error("邮箱已存在，不得重复");
                }
            }
        }

        $regioneid=$_POST["regione1"];  //地区选择
        if(!empty($_POST["regione2"])){
            $regioneid=$_POST["regione2"];
        }
        if(!empty($_POST["regione3"])){
            $regioneid=$_POST["regione3"];
        }
        if(!empty($_POST["regione4"])){
            $regioneid=$_POST["regione4"];
        }
        if(!empty($regioneid)){
            $data["manageRegione"]=$regioneid;
        }

        $aOldPwd = $_POST["aOldPwd"];
        $aNewPwd = $_POST["aNewPwd"];
        $aNewPwd2 = $_POST["aNewPwd2"];

        if($aNewPwd2!=$aNewPwd){
            $this->error("2次输入新密码不一致");
        }

            if ($aOldPwd != "" && $aNewPwd != "") {
                $rs_pwd = $admin->where("aId={$aId}")
                    ->field("aPwd")
                    ->find();
                if ($rs_pwd["aPwd"] != md5($aOldPwd)) {
                    $this->error("原密码错误");
                } else {
                    $data["aPwd"] = md5($_POST["aNewPwd"]);
                }
            } elseif ($aOldPwd != "" && $aNewPwd == "") {
                $this->error("如果不需要修改密码，请不要填写原密码");
            } elseif ($aOldPwd == "" && $aNewPwd != "") {
                $this->error("如果需要修改密码，请验证原密码");
            }

        $result = $admin->where("aId={$aId}")->save($data);
        if ($result) {
            $this->success("恭喜修改成功",U("managelists"));
        } else {
            $this->error("修改失败");
        }
    }

    public function ManageUpdateStatic()
    {
        $this->LoginTrue();
        $data["aStatic"] = $_GET["ast"];
        $aId = $_GET["aId"];
        $admin = M("admin");
        $result = $admin->where("aId={$aId}")->save($data);
        if ($result) {
            $this->success("更新状态成功");
        } else {
            $this->error("更新失败");
        }
    }

    public function ManageUpdateLock()
    {
        $this->LoginTrue();
        $data["aErrorPwdNum"] = $_GET["lock"];
        $aId = $_GET["aId"];
        $admin = M("admin");
        $result = $admin->where("aId={$aId}")->save($data);
        if ($result) {
            $this->success("设置成功");
        } else {
            $this->error("设置失败");
        }
    }

    public function ManageUpdateDepartment()
    {
        $this->LoginTrue();
        $aId = $_GET["aId"];
        $department = M("department");
        $rs_department = $department->field("dId,dPid,dPsid,dName")->select();
        $this->assign("rs_department", $rs_department);
        $admin = M("admin");
        $rs_admin = $admin->where("aId={$aId}")->find();
        $this->assign("rs_admin", $rs_admin);
        $this->display();
    }

    public function ManageUpdateDepartmentAction()
    {
        $this->LoginTrue();
        $aId = $_GET["aId"];
        $admin = M("admin");
        $data["aDid"] = $_POST["aDid"];
        $result = $admin->where("aId={$aId}")->save($data);
        if ($result) {
            $this->success("分配成功");
        } else {
            $this->error("分配失败");
        }
    }

    public function ManageDel()
    {
        $this->LoginTrue();
        $aId = $_GET["aId"];
        $this->assign("aId", $aId);
        $this->display();
    }

    public function ManageDelAction()
    {
        $this->LoginTrue();
        $aId = $_GET["aId"];
        $admin = M("admin");
        $result = $admin->where("aId={$aId}")->delete();
        if ($result) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function ShutDown()
    {
        $this->LoginTrue();
        $this->display();
    }
    //行政区列表
    public function regionelists(){
        $this->LoginTrue();
        $regione=M("regione");
        $lib=new LibController();

        $regione_list = $regione->select();

        $where=" dId > 0 ";
        if(empty($_POST["regione1"])){
            $regione1=$regione->where("pId='650000000000'")->select();
            $regionelists=$regione1;
        }
        $regioneid=$_POST["regione1"];  //地区查询
        if(!empty($_POST["regione2"])){
            $regioneid=$_POST["regione2"];
        }
        if(!empty($_POST["regione3"])){
            $regioneid=$_POST["regione3"];
        }
        if(!empty($_POST["regione4"])){
            $regioneid=$_POST["regione4"];
        }

        if(!empty($regioneid)){     //如果地区查询
            $regionelists=$regione->where("pId='{$regioneid}'")->select();

            $lib->getFatherRegione($regione_list,$regioneid);
            $flagFn=$fn=count($lib->father_Regione);
            for($fn ;$fn>0;$fn--){
                if($fn==1){
                    $regione1[]=$lib->father_Regione[$flagFn-1];
                    if($flagFn==1){
                        $pId=$lib->father_Regione[$flagFn-1]['id'];
                        $regione2=$regione->where("pId=$pId")->select();
                    }
                }
                if($fn==2){
                    $regione2[]=$lib->father_Regione[$flagFn-2];
                    if($flagFn==2){
                        $pId=$lib->father_Regione[$flagFn-2]['id'];
                        $regione3=$regione->where("pId=$pId")->select();
                    }
                }
                if($fn==3){
                    $regione3[]=$lib->father_Regione[$flagFn-3];
                    if($flagFn==3){
                        $pId=$lib->father_Regione[$flagFn-3]['id'];

                    }
                }

            }
        }

        $this->assign("regione1",$regione1);
        $this->assign("regione2",$regione2);
        $this->assign("regione3",$regione3);

        $this->assign("regionelists",$regionelists);

        $this->display();
    }
    //修改行政区名称
    public function regioneupdate(){
        $this->LoginTrue();
        $id=$_GET["id"];
        $regione=M("regione");
        $rs_regione=$regione->where("id='{$id}'")->find();
        $this->assign("rs_regione",$rs_regione);
        $this->display();
    }
    public function UpdateRegioneAction(){
        $this->LoginTrue();
        $updateId=$_GET["updateId"];
        $data["name"]=$_POST["name"];
        $regione=M("regione");
        $result=$regione->where("id='{$updateId}'")->save($data);
        if($result){
            $this->success("修改成功",U("regionelists"));
        }else{
            $this->error("修改失败");
        }
    }
    //删除行政区
    public function DelRegione(){
        $this->LoginTrue();
        $id=$_GET["id"];
        $regione=M("regione");
        $department=M("department");
        $rs_regione=$regione->where("pId='{$id}'")->select();
        if(!empty($rs_regione)){
            $this->error("该行政区下有下级行政区，无法删除，请删除所有下级后再删除");
        }
        $re_xiaozu=$department->where("pId='{$id}'")->select();
        if(!empty($re_xiaozu)){
            $this->error("该行政区下有下级小组，无法删除，请删除所有下级小组后再删除");
        }

        $result=$regione->where("id='{$id}'")->delete();
        if($result){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }

    /**
     * //添加行政区划代码
     */
    public function addRegioneAction(){
        $this->LoginTrue();
        $regione=M("regione");
        $excel=$_FILES["excel"];  //批量考勤excel表
        $objPHPExcelReader = \PHPExcel_IOFactory::load($excel["tmp_name"]);  //加载excel文件
        foreach($objPHPExcelReader->getWorksheetIterator() as $sheet)  //循环读取sheet
        {
            foreach($sheet->getRowIterator() as $row)  //逐行处理
            {
                foreach($row->getCellIterator() as $cell)  //逐列读取
                {
                    $val=$cell->getValue();
                    if(is_object($val))  $val= $val->__toString();
                    if($cell->getColumn()=='A')
                    {
                        if(!empty($val)){
                            $data[$row->getRowIndex()]['id']=$val;
                        }else{
                            break;
                        }
                    }
                    if($cell->getColumn()=='B')
                    {
                        $data[$row->getRowIndex()]['name']=$val;
                    }
                    if($cell->getColumn()=='C')
                    {
                        $data[$row->getRowIndex()]['pId']=$val;
                    }
                }
            }
        }
        foreach($data as $key => $val){
            $datenew[]= $val;
        }
        $result=$regione->addAll($datenew);
        if($result){
            $this->success("成功导入行政区",U("regionelists"));
        }else{
            $this->error("录入数据库时失败");
        }
    }

}